package com.weitu.contractor.service.impl;

import com.weitu.contractor.entity.ContractorEvaluation;
import com.weitu.contractor.entity.ContractorEvaluationCriteria;
import com.weitu.contractor.mapper.ContractorEvaluationMapper;
import com.weitu.contractor.service.ContractorEvaluationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ContractorEvaluationServiceImpl implements ContractorEvaluationService {
    private final ContractorEvaluationMapper contractorEvaluationMapper;

    private static final Logger logger = LoggerFactory.getLogger(ContractorEvaluationServiceImpl.class);

    @Autowired
    public ContractorEvaluationServiceImpl(ContractorEvaluationMapper contractorEvaluationMapper) {
        this.contractorEvaluationMapper = contractorEvaluationMapper;
    }

    @Override
    public int countByCriteria(ContractorEvaluationCriteria example) {
        int count = this.contractorEvaluationMapper.countByCriteria(example);
        logger.debug("count: {}", count);
        return count;
    }

    @Override
    public ContractorEvaluation selectByPrimaryKey(String id) {
        return this.contractorEvaluationMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<ContractorEvaluation> selectByCriteria(ContractorEvaluationCriteria example) {
        return this.contractorEvaluationMapper.selectByCriteria(example);
    }

    @Override
    @Transactional
    public int deleteByPrimaryKey(String id) {
        return this.contractorEvaluationMapper.deleteByPrimaryKey(id);
    }

    @Override
    @Transactional
    public int updateByPrimaryKeySelective(ContractorEvaluation record) {
        return this.contractorEvaluationMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    @Transactional
    public int updateByCriteriaSelective(ContractorEvaluation record, ContractorEvaluationCriteria example) {
        return this.contractorEvaluationMapper.updateByCriteriaSelective(record, example);
    }

    @Override
    @Transactional
    public int insertSelective(ContractorEvaluation record) {
        return this.contractorEvaluationMapper.insertSelective(record);
    }

    @Override
    public boolean checkIfExist(String contractorAuditId, String projectId, Integer evaluateType) {
        ContractorEvaluationCriteria evaluationCriteria = new ContractorEvaluationCriteria();
        evaluationCriteria.createCriteria()
                .andDelStateEqualTo(0)
                .andContractorAuditIdEqualTo(contractorAuditId)
                .andProjectIdEqualTo(projectId)
                .andEvaluateTypeEqualTo(evaluateType);
        return this.contractorEvaluationMapper.countByCriteria(evaluationCriteria) > 0;
    }

    @Override
    public boolean checkIfExist(String contractorAuditId, String projectId, Integer evaluateType, String id) {
        ContractorEvaluationCriteria evaluationCriteria = new ContractorEvaluationCriteria();
        evaluationCriteria.createCriteria()
                .andDelStateEqualTo(0)
                .andContractorAuditIdEqualTo(contractorAuditId)
                .andProjectIdEqualTo(projectId)
                .andEvaluateTypeEqualTo(evaluateType)
                .andIdNotEqualTo(id);
        return this.contractorEvaluationMapper.countByCriteria(evaluationCriteria) > 0;
    }

    @Override
    public boolean checkIfExist(String contractorAuditId, Integer year, Integer evaluateType) {
        ContractorEvaluationCriteria evaluationCriteria = new ContractorEvaluationCriteria();
        evaluationCriteria.createCriteria()
                .andDelStateEqualTo(0)
                .andContractorAuditIdEqualTo(contractorAuditId)
                .andYearEqualTo(year)
                .andEvaluateTypeEqualTo(evaluateType);
        return this.contractorEvaluationMapper.countByCriteria(evaluationCriteria) > 0;
    }

    @Override
    public boolean checkIfExist(String contractorAuditId, Integer year, Integer evaluateType, String id) {
        ContractorEvaluationCriteria evaluationCriteria = new ContractorEvaluationCriteria();
        evaluationCriteria.createCriteria()
                .andDelStateEqualTo(0)
                .andContractorAuditIdEqualTo(contractorAuditId)
                .andYearEqualTo(year)
                .andEvaluateTypeEqualTo(evaluateType)
                .andIdNotEqualTo(id);
        return this.contractorEvaluationMapper.countByCriteria(evaluationCriteria) > 0;
    }
}